<?php
// 后台用户模块
class CallorderAction extends CommonAction {
	function _initialize() {
            parent::_initialize();
	}

        public function index(){
		$model = M("Callorder");
		//$where['user_id'] = array('eq', $this ->get_user_id());
                
                import('ORG.Util.Page');

                $children_depts = session('children_depts');
                //var_dump(session('role_ids'));
                $rank_id = session('rank_id');
                $user_id = session('authId');
                $where = "c.user_id = u.id and u.dept_id=d.id and cu.id = c.customer_id  and u.dept_id in ($children_depts) ";
                if($rank_id == 5){//级别为普通员工时
                    $where .=" and c.user_id = $user_id ";
                }else{
                    $where .=" and u.rank_id >= $rank_id ";
                }
                $count      = $model->table('think_user u,think_callorder c,think_dept d,think_customer cu')->where($where)->count();
                $this-> _config['list_rows'] = 2;//一页展示记录数
                $Page       = new Page($count,$this-> _config['list_rows']);// 实例化分页类 传入总记录数
                $show       = $Page->show();// 分页显示输出
                $list = $model->table('think_user u,think_callorder c,think_dept d ,think_customer cu')->where($where)->limit($Page->firstRow.','.$Page->listRows)-> field('c.*,u.rank_id,u.dept_id,u.emp_name,cu.contact') -> select();
                $this->assign('page',$show);// 赋值分页输出
                $model_user = D('User');
                foreach($list as $key =>$val){
                   // echo $model_user->get_user_name($val['auditors_id']);
                    $list[$key]['auditors'] = $model_user->get_user_name($val['auditors_id']);
                    $list[$key]['audit_status_type'] = get_order_type($val['audit_status']);
                }
                $this -> assign('list', $list);
                
                
		$this -> display();
        }
        public function add(){
            $this->display();
        }
        
       	function insert() {
                $ajax = $_POST['ajax'];
		$name = $this -> getActionName();
		$model = D($name);
		if (false === $model -> create()) {
			$this -> error($model -> getError());
		}
		if (in_array('user_id', $model -> getDbFields())) {
			$model -> user_id = $this -> get_user_id();
		};
		if (in_array('user_name', $model -> getDbFields())) {
			$model -> user_name = $this -> _session("user_name");
		};
		//保存当前数据对象
                $customer_name = $_POST['customer_name'];
                $call_num = $_POST['call_num'];
                $telecom_perators = $_POST['telecom_perators'];
                $recharge_amount = $_POST['recharge_amount'];
                $annual_amount = $_POST['annual_amount']; 
                $monthly_amount = $_POST['monthly_amount'];
                $dept_id = $_POST['dept_id'];
                
                if($_POST['user_id']){
                    $user_id = $_POST['user_id'];
                    $dept_id = $_POST['dept_id'];
                }else{
                   $salesman =  session('user_name');
                   $model -> __set('salesman',  $salesman);
                   $user_id = $this -> get_user_id();
                   $dept_id = session('dept_id');
                }
                $model -> __set('addtime', time());
		$model -> __set('user_id', $user_id);
                $model -> __set('dept_id', $dept_id);
                
		$list = $model -> add();
		if ($list !== false) {//保存成功
                        /*添加子订单*/
                        $order_id = $list;
                        $node = M("CallOrderChild");
                        $lenth = count($call_num);
                        $add_time = time();
                        $edit_user_id = $this->get_user_id();
                        for($i=0;$i<=$lenth;$i++){
                           $node -> __set('customer_name',  $customer_name[$i]); 
                           $node -> __set('call_num',  $call_num[$i]);
                           $node -> __set('telecom_perators',  $telecom_perators[$i]);
                           $node -> __set('recharge_amount',  $recharge_amount[$i]);
                           $node -> __set('annual_amount',  $annual_amount[$i]);
                           $node -> __set('monthly_amount',  $monthly_amount[$i]);
                           $node -> __set('order_id', $order_id);
                           $node -> __set('add_time', $add_time);
                           $node -> __set('edit_user_id', $edit_user_id);
                           $node_list = $node -> add();
                        }
                        /*添加子订单结束**/
                        $this -> success('新增成功!');
			$this -> assign('jumpUrl', $this -> get_return_url());
		} else {
			//失败提示
			$this -> error('新增失败!');
		}
	}
        
        function edit() {
		$name = $this -> getActionName();
		$model = D($name);
		$id = $_REQUEST[$model -> getPk()];
		$vo = $model -> getById($id);
                $model_user = D('User');
                $vo['salesman'] = $model_user->get_user_name($vo['user_id']);
                $model_customer = D('Customer');
                $vo['customer_name'] = $model_customer->get_customer_name($vo['customer_id']);
                $callorder_childreen = $model->get_callorder_child($vo['id']);
                $vo['count_childreen'] = count($callorder_childreen);
                $this -> assign('callorder_childreen', $callorder_childreen);
		$this -> assign('vo', $vo);
		$this -> display();
	}

        	function update() {
		$ajax = $_POST['ajax'];
		$id = $_POST['id'];
		$model = D("Callorder");
		if (false === $model -> create()) {
			$this -> error($model -> getError());
		}
		// 更新数据
		$list = $model -> save();
		if (false !== $list) {
			//成功提示
			//$this -> assign('jumpUrl', $this -> get_return_url());
                        if($ajax == 1){
			$this -> success('编辑成功!');
                        }else{
                        $this -> assign('jumpUrl', $this -> get_return_url());
                        }
		} else {
			//错误提示
			$this -> error('编辑失败!');
		}
	}
        
           function audit() {
		$name = $this -> getActionName();
		$model = D($name);
		$id = $_REQUEST[$model -> getPk()];
		$vo = $model -> getById($id);
                $model_user = D('User');
                $vo['salesman'] = $model_user->get_user_name($vo['user_id']);
                $vo['auditors_name'] = $model_user->get_user_name($vo['auditors_id']);
                $model_customer = D('Customer');
                $vo['customer_name'] = $model_customer->get_customer_name($vo['customer_id']);
                $callorder_childreen = $model->get_callorder_child($vo['id']);
                $vo['count_childreen'] = count($callorder_childreen);
                
                $this -> assign('callorder_childreen', $callorder_childreen);
		$this -> assign('vo', $vo);
		$this -> display();
	}
        
        function auditDone(){
                $name = $this -> getActionName();
		$model = D($name);
               
                $this -> success('审核成功!'); 
        }

        protected function get_list_rows() {
		return $this -> _config['list_rows'];
	}

	protected function set_list_rows($num) {
		$this -> _list_rows = $num;
	}

}
?>